<?php
    $title = "Fiche employés";
    $this->headTitle($title);

    // Variables
    $site	 = $this->site;
    $refresh = $this->refresh;
    $codes	 = $this->codes;
    $codec	 = $this->codec;
?>




<div class="content-head">
   <h3>Fiche employés du site <a id='site' href='#'  data-type="select"><?php echo $site->libelle; ?></a>
       
   </h3>
   
    <div>	
    </div>
    <form action="#" method="post" style="display: none;">
	<input type="hidden" name="site_id" id="data-site" value="<?php echo $site->id; ?>">
        <input type="hidden" name="page" id="data-page" value="<?php echo $this->page; ?>">        
	<input type="hidden" name="perpage" id="data-perpage" value="<?php echo $this->perPage; ?>">
        <input type="hidden" name="page" id="data-col-order" value="">
        <input type="hidden" name="page" id="data-col-order-state" value="">
    </form>
</div>

<div class="content-body">    
    <!-- CONTENU AJAX -->
    <form id='form-fiche-filter'>
        <div style='margin-bottom: 8px;'>
            <input type=text' name='filter-nom' id='filter-nom' value=''  autocomplete="off" placeholder="NOM / PRENOM"  style="height:25px; width:47%; padding-left: 6px;">    
            <button type="submit" class="btn" id='btn-filter-submit'  style=" width:15%;"><i class="icon-search icon-large"></i> </button> 
            <button type="button" class="btn" id='btn-plus-critere'  style=" width:25%; float: right;" data-state='plus'>Plus de critères<i class="icon-plus-sign icon-small" style='float:right; '></i> </button> 
        </div>
        <div class='criteria-cntr'>
            <select id='filter-active' name='filter-active'    style='height:32px;'>
                <option value ='-1'>Tous les employés</option>
                <option value ='1' selected>Employés en cours</option>                
                <option value ='0'> Anciens employés</option>                
            </select>
            <select id='filter-contrat'  style=height:31px;'>
                <option value ='0'>Tous les type de contrats</option>                
                <?php foreach ($listContratType as $contratType): ?>
                    <option value ='<?php echo $contratType->id ;?>'><?php echo $contratType->libelle ;?></option>                
                <?php endforeach; ?>
            </select>
            <select id='filter-poste'  style=height:31px;' name='filter-poste'>
                <option value ='-1'>Tous les postes</option>                
                <?php foreach ($listPoste as $poste): ?>
                    <option value ='<?php echo $poste->id ;?>'><?php echo $poste->code ;?> - <?php echo $poste->libelle ;?></option>                
                <?php endforeach; ?>
            </select>
        </div>   
        <!--input type="checkbox" value="at" id='filter-active' name='filter-active' checked> &nbsp;Actif 
        <input type="checkbox" value="at" id='filter-inactive' name='filter-inactive' > &nbsp;Inactif<br-->
        <!--a onclick="" style="float: right;" class="btn btn-info btn-small" id="" href="#">Plus de critères <i class="icon-white icon-plus-sign"></i></a-->
    </form>
    <div id='content-data'>
	<!-- AJAX LOADING -->
	
	
    </div>

</div>

<script type="text/javascript">
    $(document).ready(function() {
       var $filterContrat = $('select#filter-contrat'),
               $filterPoste = $('select#filter-poste'),
               $filterActive    = $('select#filter-active'),
               $btnCriteria = $("#btn-plus-critere")
               ;
       
       refresh(); 
       
      /* $("#filter-nom").on('change', function(){
          console.log('change');
       });*/
        
       $btnCriteria.on('click', function(){
           var $this = $(this),
               state = $this.attr('data-state') ;
           
           $('.criteria-cntr').toggle(200);
           //init default value
           $filterContrat.val(0);
           $filterPoste.val(-1);
           $filterActive.val(1);
           
           if(state === 'plus')
               $this.html("Moins de critères<i class='icon-minus-sign icon-small' style='float:right; '></i> ");           
           else{
               $this.html("Plus de critères<i class='icon-plus-sign icon-small' style='float:right; '></i>  ");           
               refresh({page : 1, callback : function(){
                       console.log('callback');}
               });
           }
           
           $this.attr('data-state', state === 'plus'? 'minus': 'plus');
           //$("#form-fiche-filter")[0].reset();
       });
       $('#btn-filter-submit').on('click', function(e){
           var $this = $(this),
               $filterNom = $('input#filter-nom')
               ; 
          
           e.preventDefault();
           
           nomTrim =  $.trim($filterNom.val());           
           nomTrim = nomTrim.replace(new RegExp("( )+", "g"), ' ');
           
           $filterNom.val(nomTrim);
           refresh();
       });
       
       $filterActive.change(function(){
           refresh();
       });
       
       $filterPoste.change(function(){
           refresh();
       });
       $filterContrat.change(function(){
           refresh();
       });
       
       
       $('#site').editable({
            title : 'Liste des Sites',
            source : '<?php echo $this->url('RHX/default', array('controller'	 => 'employe', 'action'	 => 'jsonSiteList')) ?>',
            validate: function(value){
            $('#data-site').val(value);
            refresh();
            changeSite(value);
            }
        }); 
    
    });// end ready
    
    /* rafraichir liste des employé
     * @return (Boolean)
     */
    function refresh_old(){
        var siteId  = $('#data-site').val(),            
            perpage = $('#data-perpage').val(),
            
            $filterNom      = $('input#filter-nom'),
            $filterContrat  = $('select#filter-contrat'),
            $filterPoste    = $('select#filter-poste'),
            $filterActive    = $('select#filter-active')
            ;
        
        nom           = $filterNom.val() !== ""        ? $filterNom.val()               : null;
        contrat_type  = $filterContrat.val()    !== '0'? parseInt($filterContrat.val()) : null;
        poste         = $filterPoste.val()      !== '-1'? parseInt($filterPoste.val())   : -1;;
        
        dataColOrder = $('#data-col-order').val() !== '' ?  $('#data-col-order').val() : null;
        dataColOrderState = $('#data-col-order-state').val() !== '' ?  $('#data-col-order-state').val() : null;
        active = $filterActive.val();
        
        //serialize data post
        _data = ''; 
        
        _data += '&actif=' + active;        
        
        if(nom)
            _data += '&nom='+ nom;
        if(contrat_type)
            _data += '&contrat_type='+ contrat_type;
        if(poste != -1)
            _data += '&poste='+ poste;
        
        if(dataColOrder && dataColOrderState)
            _data += '&order_col='+ dataColOrder+ '&order_col_state='+ dataColOrderState;
        
        console.log(_data);
        
       
        ajaxPreLoad = $.ajax({
	    type: 'post',
	    url : '<?php echo $this->url('RHX/default', array('controller'	 => 'fiche', 'action'	 => 'loadIndex')); ?>/' + siteId + '/1/' + page + '/' + perpage,
            data: _data            
	});
	ajaxPreLoad.success(function(data){
	    $('#content-data').hide().html(data).fadeIn('normal');
	});
	ajaxPreLoad.error(function(){
	    $('#content-data').html("<div class='rh-error'>Contenu Impossible à charger</div>");
	});
    }
    
    /* rafraichir liste des employé
     * @return (Boolean)
     */
    function refresh(paramCall){
    
        var defautsParam =  {
                                "page" : 1,
                                "callback" : null
                            };
           
        var siteId  = $('#data-site').val(),            
            perpage = $('#data-perpage').val(),
            
            $filterNom      = $('input#filter-nom'),
            $filterContrat  = $('select#filter-contrat'),
            $filterPoste    = $('select#filter-poste'),
            $filterActive    = $('select#filter-active')
            ;
        
        param = $.extend(defautsParam, paramCall);
        
        nom           = $filterNom.val() !== ""        ? $filterNom.val()               : null;
        contrat_type  = $filterContrat.val()    !== '0'? parseInt($filterContrat.val()) : null;
        poste         = $filterPoste.val()      !== '-1'? parseInt($filterPoste.val())   : -1 ;
        
        dataColOrder = $('#data-col-order').val() !== '' ?  $('#data-col-order').val() : null;
        dataColOrderState = $('#data-col-order-state').val() !== '' ?  $('#data-col-order-state').val() : null;
        active = $filterActive.val();
        
        //serialize data post
        _data = ''; 
        
        _data += '&actif=' + active;        
        
        if(nom)
            _data += '&nom='+ nom;
        if(contrat_type)
            _data += '&contrat_type='+ contrat_type;
        if(poste > -1)
            _data += '&poste='+ poste;
        
        if(dataColOrder && dataColOrderState)
            _data += '&order_col='+ dataColOrder+ '&order_col_state='+ dataColOrderState;
        
        console.log(_data);
        
        ajaxPreLoad = $.ajax({
	    type: 'post',
	    url : '<?php echo $this->url('RHX/default', array('controller'	 => 'fiche', 'action'	 => 'loadIndex')); ?>/' + siteId + '/1/' + param.page + '/' + perpage,
            data: _data            
	});
	ajaxPreLoad.success(function(data){
	    $('#content-data').hide().html(data).fadeIn('normal');
            if( param.callback)
                param.callback();
	});
	ajaxPreLoad.error(function(){
	    $('#content-data').html("<div class='rh-error'>Contenu Impossible à charger</div>");
	});
    }
</script>